package net.chengp.ms.auth.filter;

import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter;
import org.springframework.security.web.AuthenticationEntryPoint;

/**
 * 重写客户端认证失败异常
 * @author chengp
 *
 */
public class CustomClientCredentialsTokenEndpointFilter extends ClientCredentialsTokenEndpointFilter {

	private AuthorizationServerSecurityConfigurer configurer;
	private AuthenticationEntryPoint authenticationEntryPoint;

	public CustomClientCredentialsTokenEndpointFilter(AuthorizationServerSecurityConfigurer configurer) {
		this.configurer = configurer;
	}

	@Override
	public void setAuthenticationEntryPoint(AuthenticationEntryPoint authenticationEntryPoint) {
		super.setAuthenticationEntryPoint(null);
		this.authenticationEntryPoint = authenticationEntryPoint;
	}

	@Override
	protected AuthenticationManager getAuthenticationManager() {
		return configurer.and().getSharedObject(AuthenticationManager.class);
	}

	@Override
	public void afterPropertiesSet() {
		setAuthenticationFailureHandler(
				(request, response, e) -> authenticationEntryPoint.commence(request, response, e));
		setAuthenticationSuccessHandler((request, response, authentication) -> {
		});
	}
}